package com.its.us.web.config.handler;

import com.its.us.web.utils.ConstantsUtil;
import com.its.us.web.utils.JsonUtil;
import com.its.us.web.utils.ResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 匿名登录访问
 *
 * @Author: master
 * @Date: 2020/10/28 19:57
 * @Version: 1.0
 */
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Map<String, Object> result = new HashMap<>();
        result.put("code", ConstantsUtil.HttpStatus.UNSUPPORTED);
        result.put("msg", "匿名访问需要身份认证");
        ResponseUtil.out(415, httpServletResponse, JsonUtil.toJson(result));
    }
}
